package com.haina.vote.controller;
import com.github.pagehelper.PageInfo;
import com.haina.vote.entity.KqFwModel;
import com.haina.vote.entity.KqFwPerformance;
import com.haina.vote.enums.TaskStatusEnum;
import com.haina.vote.service.IKqFwService;
import com.haina.vote.utils.RandomCodeUtils;
import org.junit.platform.commons.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.*;


@Controller
@RequestMapping("/kqfw")
public class kqfwController {
    @Autowired
    private IKqFwService kqFwService;

    //跳转到新建科情服务单页面
    @RequestMapping(value = "/kqfwaddUI.do")
    public ModelAndView addUI(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("kqfwtask-add");
        return mv;
    }

    //进入前台列表页
    @RequestMapping(value = "/findTaskUI.do")
    public ModelAndView findTaskUI(@RequestParam(name = "page",required = true,defaultValue = "1")Integer page,@RequestParam(name = "pageSize",required = true,defaultValue = "10")Integer pageSize){
        List<KqFwModel> kqFwModelList=kqFwService.findTask(page,pageSize);
        PageInfo pageInfo=new PageInfo(kqFwModelList);
        ModelAndView mv=new ModelAndView();
        mv.setViewName("/UI/kqfwtask-UI");
        mv.addObject("pageInfo",pageInfo);
        mv.addObject("mes","首页");
        return mv;
    }
    //进入前台搜索
    @RequestMapping(value = "/findTaskByNameUI.do")
    public ModelAndView findTaskByNameUI(@RequestParam(name = "page",required = true,defaultValue = "1")Integer page,@RequestParam(name = "pageSize",required = true,defaultValue = "10")Integer pageSize,@RequestParam(name = "tl")String tl){
        if (StringUtils.isNotBlank(tl)){
            List<KqFwModel> kqFwModelList=kqFwService.findTaskByName(page,pageSize,tl);
            PageInfo pageInfo=new PageInfo(kqFwModelList);
            ModelAndView mv=new ModelAndView();
            mv.setViewName("/UI/kqfwtask-UI");
            mv.addObject("pageInfo",pageInfo);
            mv.addObject("mes","搜索页");
            return mv;
        }else {
            List<KqFwModel> kqFwModelList=kqFwService.findTask(page,pageSize);
            PageInfo pageInfo=new PageInfo(kqFwModelList);
            ModelAndView mv=new ModelAndView();
            mv.setViewName("/UI/kqfwtask-UI");
            mv.addObject("pageInfo",pageInfo);
            mv.addObject("mes","首页");
            return mv;
        }
    }

    //进入后台搜索
    @RequestMapping(value = "/findTaskByNameAdmin.do")
    public ModelAndView findTaskByName(@RequestParam(name = "page",required = true,defaultValue = "1")Integer page,@RequestParam(name = "pageSize",required = true,defaultValue = "10")Integer pageSize,@RequestParam(name = "tl")String tl){
        if (StringUtils.isNotBlank(tl)){
            List<KqFwModel> kqFwModelList=kqFwService.findTaskByName(page,pageSize,tl);
            PageInfo pageInfo=new PageInfo(kqFwModelList);
            ModelAndView mv=new ModelAndView();
            mv.setViewName("kqfwtask-list");
            mv.addObject("pageInfo",pageInfo);
            return mv;
        }else {
            List<KqFwModel> kqFwModelList=kqFwService.findTask(page,pageSize);
            PageInfo pageInfo=new PageInfo(kqFwModelList);
            ModelAndView mv=new ModelAndView();
            mv.setViewName("kqfwtask-list");
            mv.addObject("pageInfo",pageInfo);
            return mv;
        }
    }


    //进入后台列表页
    @RequestMapping(value = "/findTaskAdmin.do")
    public ModelAndView findTaskAdmin(@RequestParam(name = "page",required = true,defaultValue = "1")Integer page,@RequestParam(name = "pageSize",required = true,defaultValue = "10")Integer pageSize){
        List<KqFwModel> kqFwModelList=kqFwService.findTask(page,pageSize);
        PageInfo pageInfo=new PageInfo(kqFwModelList);
        ModelAndView mv=new ModelAndView();
        mv.setViewName("kqfwtask-list");
        mv.addObject("pageInfo",pageInfo);
        return mv;
    }

    //新建科情服务任务单
    @PostMapping("/addTask.do")
    public ModelAndView addTask(KqFwModel kqFwModel, @RequestParam(name = "person_in_charge")String[] person_in_charge,@RequestParam(name = "level")String[] level,@RequestParam(name = "task_allocation")String[] task_allocation){
        kqFwModel.setStatus(TaskStatusEnum.SUBMIT.getCode());
        kqFwModel.setCreate_time(new Date());
        //生成六位数的随机码 并返回到页面
        String code=RandomCodeUtils.RandomCode();
        kqFwModel.setCode(code);
        //生成编号1、首先查询数据库当前最大编号 2、处理编号
        KqFwModel taskMaxTime = kqFwService.findTaskMaxTime();
        //调用编号生成方法
        String newNumber = RandomCodeUtils.findNewTaskNo(taskMaxTime);
        kqFwModel.setNumber(newNumber);
        Integer id=kqFwService.addTask(kqFwModel);
        ModelAndView mv=new ModelAndView();
        if (id!=null&&id!=-1){
            KqFwPerformance kqFwPerformance=new KqFwPerformance();
            for (int i=0;i<person_in_charge.length;i++){
                kqFwPerformance.setPerson_in_charge(person_in_charge[i]);
                kqFwPerformance.setLevel(level[i]);
                kqFwPerformance.setTask_allocation(task_allocation[i]);
                kqFwPerformance.setTaskId(id);
                kqFwService.addPerformance(kqFwPerformance);
            }
        }
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("kqfwtask-add-sucess");
        modelAndView.addObject("code",code);
        return modelAndView;
    }

    //删除科情服务任务单
    @GetMapping("/deleteTask.do")
    public String deleteTask(@RequestParam(name = "id")Integer id){
        kqFwService.deleteTask(id);
        return "redirect:findTaskAdmin.do";
    }


    //查询需要修改的信息并回显
    @GetMapping("/updateTaskUI.do")
    public ModelAndView updateTaskUI(@RequestParam("id")Integer id){
        KqFwModel kqFwModel = kqFwService.findTaskById(id);
        ModelAndView mv = new ModelAndView();
        mv.addObject("kqfwtask",kqFwModel);
        mv.setViewName("kqfwtask-edit");
        return mv;
    }

    //修改科情服务任务单
    @PostMapping("/updateTask.do")
    public String updateTask(KqFwModel kqFwModel,HttpServletRequest request){
        kqFwModel.setUpdate_time(new Date());
        System.out.println(kqFwModel);
        kqFwService.updateTask(kqFwModel);
        if (kqFwModel.getId()!=null&&kqFwModel.getId()!=-1){
            KqFwPerformance kqFwPerformance=new KqFwPerformance();
            Map map= request.getParameterMap();
            System.out.println(map);
            if (map.get("person_in_charge")!=null){
                String[] pid= (String[]) map.get("pid");
                String[] person_in_charge= (String[]) map.get("person_in_charge");
                String[] level= (String[]) map.get("level");
                String[] task_allocation= (String[]) map.get("task_allocation");
                for (int i=0;i<person_in_charge.length;i++){
                    if (Integer.parseInt(pid[i])==0){
                        kqFwPerformance.setPerson_in_charge(person_in_charge[i]);
                        kqFwPerformance.setLevel(level[i]);
                        kqFwPerformance.setTask_allocation(task_allocation[i]);
                        kqFwPerformance.setTaskId(kqFwModel.getId());
                        kqFwService.addPerformance(kqFwPerformance);
                    }else {
                        kqFwPerformance.setPerson_in_charge(person_in_charge[i]);
                        kqFwPerformance.setLevel(level[i]);
                        kqFwPerformance.setTask_allocation(task_allocation[i]);
                        kqFwPerformance.setTaskId(kqFwModel.getId());
                        kqFwService.updatePerformance(kqFwPerformance);
                    }

                }

            }

        }
        return "redirect:findTaskAdmin.do";
    }
    //查看科情服务单详情
    @GetMapping("/taskInfo.do")
    public ModelAndView taskInfo(@RequestParam(name = "id")Integer id){
        KqFwModel kqFwModel=kqFwService.findTaskById(id);
        ModelAndView mv = new ModelAndView();
        mv.addObject("taskInfo",kqFwModel);
        mv.setViewName("kqfwtask-show");
        return mv;
    }
    //根据处理参数修改任务单的状态
    @GetMapping("/setStatus")
    public String setStatus(@RequestParam(name = "id") Integer id,@RequestParam(name = "status") Integer status){
         kqFwService.setStatus(id,status);
        return "redirect:findTaskAdmin.do";

    }
    //上传反馈资料
    //创建选项
    @RequestMapping("/uploadMeans.do")
    public String saveMeans(HttpServletRequest request, MultipartFile upload,@RequestParam(name = "id")Integer id) throws Exception {
        //使用fileupload组件完成文件上传
        //上传位置
        String path =request.getSession().getServletContext().getRealPath("/uploads/kqfw");
        String filename=null;
        //判断路径是否存在
        File file=new File(path);
        if (!file.exists()){
            file.mkdirs();
        }
        filename = upload.getOriginalFilename();
        //String uuid = UUID.randomUUID().toString().replace("-","");
        filename=filename;
        //完成文件上传
        upload.transferTo(new File(path,filename));
        String fileurl="/uploads/kqfw/"+filename;
        KqFwModel param =new KqFwModel();
        param.setMeans_url(fileurl);
        param.setId(id);
        kqFwService.updateMeansUrl(param);
        return "redirect:findTaskAdmin.do";
    }

}
